package com.weipai.springboot.model.api;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author songmengqiang
 * @create 2018-09-25 19:22
 **/

public class CamelMap extends HashMap<String, Object> {
    @Override
    public Object put(String key, Object value) {
        return super.put(underline2Camel(key), value);
    }

    /**
     * 下划线转驼峰法
     *
     * @param line 源字符串
     * @return 转换后的字符串
     */
    public String underline2Camel(String line) {
        if (line == null || "".equals(line)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("(_)?([A-Za-z\\d]+)");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            int index = word.lastIndexOf('_');
            if (index == 0) {
                sb.append(Character.toUpperCase(word.charAt(1)));
                sb.append(word.substring(2));
            } else {
                sb.append(word);
            }
        }
        return sb.toString();
    }
}
